//
//  WeChatCell.m
//  HanTu
//
//  Created by apple on 2016/11/14.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "WeChatCell.h"
#import "MessageModel.h"
#import "Masonry.h"

#define LEFT_WITH (AppFrameWidth > 750 ? 55:55)

#define RIGHT_WITH (AppFrameWidth > 750 ? 89:73)

// 1s  length 75
// 60s  6~197 6p~230

#define MAX_IMAGE_WH 141.0

#define DEF_ICON @"collection4_10"

@interface WeChatCell ()

@property (nonatomic, strong) UILabel *timeLabel;

@property (nonatomic, strong) UIImageView *logoImage;

@property (nonatomic, strong) UILabel *textMessageLabel;

@property (nonatomic, strong) UIImageView *contextBack;

@end

@implementation WeChatCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // 发送的时间
        UILabel *timeLabel=[[UILabel alloc] init];
        timeLabel.font=[UIFont fontWithName:FONT_REGULAR size:10];
        timeLabel.backgroundColor=COLOR_cecece;
        timeLabel.textColor=COLOR_ffffff;
        timeLabel.textAlignment=NSTextAlignmentCenter;
        timeLabel.layer.masksToBounds=YES;
        timeLabel.layer.cornerRadius=4;
        timeLabel.layer.borderColor=[COLOR_cecece CGColor];
        timeLabel.layer.borderWidth=1;
        [self.contentView addSubview:timeLabel];
        self.timeLabel = timeLabel;

        // 用户的头像
        UIImageView *logoImage=[[UIImageView alloc] init];
        [self.contentView addSubview:logoImage];
        self.logoImage = logoImage;

        // 背景图片
        UIImageView *contextBack = [[UIImageView alloc] init];
        contextBack.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGRClickAction:)];
        [contextBack addGestureRecognizer:tapGR];
        [self.contentView addSubview:contextBack];
        self.contextBack = contextBack;
        
        // 文字内容
        UILabel *textMessageLabel=[[UILabel alloc] init];
        textMessageLabel.numberOfLines=0;
        textMessageLabel.lineBreakMode=NSLineBreakByWordWrapping;
        textMessageLabel.font = [UIFont fontWithName:FONT_REGULAR size:14.0];;
        textMessageLabel.textColor=COLOR_444444;
        [self.contentView addSubview:textMessageLabel];
        self.textMessageLabel = textMessageLabel;
        


    }
    
    return self;
}

/**
 * @brief 数据模型
 */
- (void)setModel:(MessageModel *)model{

    _model = model;
    
    self.timeLabel.hidden = !model.showMessageTime;

    CGFloat masTop= (model.showMessageTime ? 37 : 10);

    // 时间
    self.timeLabel.frame = CGRectMake((KScreenWidth - 40)/2, 10, 40, 17);
    self.timeLabel.text = model.messageTime;

    // 文字内容的大小
    CGFloat maxWith=AppFrameWidth-LEFT_WITH-RIGHT_WITH-14-12-4;
    UIFont *textFont=[UIFont fontWithName:FONT_REGULAR size:14.0];
    NSDictionary *attributes = @{NSFontAttributeName: textFont};
    CGRect rect = [model.messageText boundingRectWithSize:CGSizeMake(maxWith, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:attributes context:nil];

    if (model.messageSenderType==MessageSenderTypeHantu) // 商家
    {
        // 用户的头像
        self.logoImage.frame=CGRectMake(10, masTop, 40, 40);
        [self.logoImage sd_setImageWithURL:[NSURL URLWithString:model.logoUrl] placeholderImage:[UIImage imageNamed:DEF_ICON]];

        if (model.messageType==MessageTypeText)  // 内容是文字
        {
            // 隐藏图片
            self.textMessageLabel.hidden = NO;

            // 发送的文字
            self.textMessageLabel.text=model.messageText;
            self.textMessageLabel.frame=CGRectMake(LEFT_WITH+12, masTop+14, rect.size.width, rect.size.height);

            // 气泡
            self.contextBack.image=[[UIImage imageNamed:@"wechatback1"] stretchableImageWithLeftCapWidth:10 topCapHeight:25];
            self.contextBack.frame=CGRectMake(LEFT_WITH, masTop, rect.size.width+26, rect.size.height+26);

        }
        else if (model.messageType==MessageTypeImage) // 内容是图片
        {
            // 隐藏文字
            self.textMessageLabel.hidden = YES;

            // 气泡
            CGSize imageSize=[WeChatCell imageShowSize:model.imageSmall];
            self.contextBack.frame = CGRectMake(LEFT_WITH, masTop, imageSize.width, imageSize.height);
            self.contextBack.image = model.imageSmall;
            
            // 将文字内容的frame设置为0，防止重用
            self.textMessageLabel.frame = CGRectMake(0, 0, 0, 0);
        }
    }
    else // 用户
    {
        // 头像
        self.logoImage.frame=CGRectMake(AppFrameWidth-10-40, masTop, 40, 40);
        [self.logoImage sd_setImageWithURL:[NSURL URLWithString:model.logoUrl] placeholderImage:[UIImage imageNamed:DEF_ICON]];

        if (model.messageType==MessageTypeText)  // 内容是文字
        {
            // 隐藏图片
            self.textMessageLabel.hidden = NO;

            // 文字
            self.textMessageLabel.frame=CGRectMake(AppFrameWidth-(rect.size.width+26)-LEFT_WITH+12, masTop+14, rect.size.width, rect.size.height);
            self.textMessageLabel.text=model.messageText;

            // 气泡
            self.contextBack.image=[[UIImage imageNamed:@"wechatback2"] stretchableImageWithLeftCapWidth:10 topCapHeight:25];
            self.contextBack.frame=CGRectMake(AppFrameWidth-(rect.size.width+26)-LEFT_WITH, masTop, rect.size.width+26, rect.size.height+26);

        }
        else if (model.messageType==MessageTypeImage) // 内容是图片
        {
            // 隐藏文字
            self.textMessageLabel.hidden = YES;

            // 气泡
            CGSize imageSize = [WeChatCell imageShowSize:model.imageSmall];
            self.contextBack.frame = CGRectMake(AppFrameWidth - LEFT_WITH - imageSize.width, masTop, imageSize.width, imageSize.height);
            self.contextBack.image = model.imageSmall;
            
            // 将文字的Label的frame设置成0，防止重用
            self.textMessageLabel.frame = CGRectMake(0, 0, 0, 0);
        }
    }
}

+(CGFloat)tableHeightWithModel:(MessageModel *)model{
    
    CGFloat masTop=10;
    
    if (model.showMessageTime)
    {
        masTop=37;
    }

     if (model.messageType==MessageTypeText)
     {
        CGFloat maxWith=AppFrameWidth-LEFT_WITH-RIGHT_WITH-14-12-4;
        
        UIFont *textFont=[UIFont fontWithName:FONT_REGULAR size:14];
        
        NSDictionary *attributes = @{NSFontAttributeName: textFont};
        
        CGRect rect = [model.messageText boundingRectWithSize:CGSizeMake(maxWith, MAXFLOAT)  options:NSStringDrawingUsesLineFragmentOrigin  attributes:attributes context:nil];

        return rect.size.height+26+masTop+20;
    }
     else if (model.messageType==MessageTypeVoice)
     {
          return 40+masTop+20;
     }
     else if (model.messageType==MessageTypeImage)
     {
         
        CGSize cgsize=[self imageShowSize:model.imageSmall];
         
         return 40+cgsize.height+20;
     }

    return 0;
}

/**
 * @brief 判断图片长度&宽度
 */
+(CGSize)imageShowSize:(UIImage *)image{
    
    CGFloat imageWith=image.size.width;
    
    CGFloat imageHeight=image.size.height;
    
    //宽度大于高度
    if (imageWith>=imageHeight) {
        
        // 宽度超过标准宽度
        /*
        if (imageWith>MAX_IMAGE_WH) {
           
        }else{
            
        }
         */
        
        
         return CGSizeMake(MAX_IMAGE_WH, imageHeight*MAX_IMAGE_WH/imageWith);
        
        
    }else{
        /*
        if (imageHeight>MAX_IMAGE_WH) {
            
        }else{
            
        }
        */
        
        
        return CGSizeMake(imageWith*MAX_IMAGE_WH/imageHeight, MAX_IMAGE_WH);
        
    }
    
    
    
    
    
    return CGSizeZero;
}

/**
 * @brief 聊天图片的点击
 */
- (void)tapGRClickAction:(UITapGestureRecognizer *)tapGR{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(ClickOnTheChatPicture:)]) {
        
        [self.delegate ClickOnTheChatPicture:tapGR];
    }
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

@end
